# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children if children is not None else []


class Solution:
    def __init__(self):
        self.ans = 0

    def diameter(self, root: 'Node') -> int:
        self.dfs(root)
        return self.ans

    def dfs(self, node):
        if not node:
            return 0

        max1 = max2 = 0
        for child in node.children:
            val = self.dfs(child)
            if val > max1:
                max1, max2 = val, max1
            elif val > max2:
                max1, max2 = max1, val

        self.ans = max(self.ans, max1 + max2)

        return max1 + 1


if __name__ == "__main__":
    pass
